<?php

namespace App\Http\Controllers\Mobile\Personal;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Style;

class BrowseJobController extends CommonController{

    /**
     * 浏览职位记录
     * @param Request $request
     * @return mixed
     */
    public function browseJob(Request $request){
        $user = auth()->guard('personal')->user();
        //关键词搜索
        $keyword  = trim($request->keyword);
        $browse_job = DB::table('browse_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_users as t3', 't1.company_uid', '=', 't3.uid')
            ->join('company_job as t4', 't1.job_id', '=', 't4.id')
            ->select(
                't1.id','t1.created_at','t1.job_id','t1.com_id',
                't2.company_name','t2.region','t2.industry','t2.region',
                't3.last_login_time',
                't4.job_title','t4.salary_above','t4.salary_below','t4.location1'
            )
            ->where('t1.personal_uid',$user->uid)
            ->where(function ($query) use ($keyword)  {
                if(strlen($keyword) > 0){
                    $query->where('t2.company_name','like','%'.$keyword.'%');
                }
            })
            ->orderBy('created_at', 'desc')
            ->paginate(10);
        //dd($browse_job);
        //处理数组
        foreach($browse_job as $v){
            $v->job_title = str_limit($v->job_title,24);
            $v->company_name = str_limit($v->company_name,26);
            $v->region = str_limit(str_replace(',','',CollArea::getCityAddress($v->region)),17);
            $v->location1  = ($v->location1) ?  CollArea::getAreaName($v->location1) : '';
            $v->created_at = substr($v->created_at,0,16);
            $v->last_login_time = substr($v->last_login_time,0,16);
            $v->salary_above = ($v->salary_above) ? CollOption::getOptionName($v->salary_above,'expectedsalary') : '';
            $v->salary_below = ($v->salary_below) ? CollOption::getOptionName($v->salary_below,'expectedsalary') : '';
        }
        //分页分配参数 过滤空值
        $appends =  arrayFilter( array(
            'keyword' => $keyword
        ));
        return Style::view('mobile.personal.browseJob',compact('browse_job','appends'));
    }
}